package store.lunangangster.engine.impl.db;

import java.util.ArrayList;
import java.util.List;
import store.lunangangster.engine.impl.entity.ByteArrayEntityImpl;
import store.lunangangster.engine.impl.entity.DeploymentEntityImpl;
import store.lunangangster.engine.impl.entity.Entity;
import store.lunangangster.engine.impl.entity.ExecutionEntityImpl;
import store.lunangangster.engine.impl.entity.HistoryEntityImpl;
import store.lunangangster.engine.impl.entity.IdentityLinkEntityImpl;
import store.lunangangster.engine.impl.entity.MesListenerEntityImpl;
import store.lunangangster.engine.impl.entity.ProcessDefinitionEntityImpl;
import store.lunangangster.engine.impl.entity.ResourceEntityImpl;
import store.lunangangster.engine.impl.entity.TaskEntityImpl;
import store.lunangangster.engine.impl.entity.VariableEntityImpl;

/**
 * 该类维护了所有{@link Entity}在数据库中的依赖关系，该关系决定了在DML操作时的先后顺序
 *
 * @author cwk
 * @version 1.0
 */
public class EntityDependencyOrder {

  public static final List<Class<? extends Entity>> INSERT_ORDER = new ArrayList<>();

  static {
    initInsertOrder();
  }

  private static void initInsertOrder() {
    INSERT_ORDER.add(DeploymentEntityImpl.class);
    // fk: NF_RE_DEPLOYMENT <== NF_RE_PRODEFINE
    INSERT_ORDER.add(ProcessDefinitionEntityImpl.class);
    // fk: NF_RE_DEPLOYMENT <== NF_RE_RESOURCE
    INSERT_ORDER.add(ResourceEntityImpl.class);
    // fk: NF_RE_DEPLOYMENT <== NF_RU_EXECUTION
    // fk: NF_RE_PRODEFNIE  <== NF_RU_EXECUTION
    INSERT_ORDER.add(ExecutionEntityImpl.class);
    // fk: NF_RE_DEPLOYMENT <== NF_RU_TASK
    // fk: NF_RE_PRODEFINE  <== NF_RU_TASK
    // fk: NF_RU_EXECUTION  <== NF_RU_TASK
    INSERT_ORDER.add(TaskEntityImpl.class);
    // fk: NF_RU_TASK       <== NF_RU_IDENTITYLINK
    INSERT_ORDER.add(IdentityLinkEntityImpl.class);
    // fk: NF_RU_BYTEARRAY  <== NF_RU_VARIABLE
    INSERT_ORDER.add(ByteArrayEntityImpl.class);
    // fk: NF_RU_EXECUTION  <== NF_RU_VARIABLE
    INSERT_ORDER.add(VariableEntityImpl.class);
    // fk: NF_RU_EXECUTION  <== NF_RU_MESLISTENER
    INSERT_ORDER.add(MesListenerEntityImpl.class);

    // history
    INSERT_ORDER.add(HistoryEntityImpl.class);
  }

}
